package com.leoandroid.tool.toolsbox.utils

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.TTSplashAd
import com.leoandroid.tool.toolsbox.largeread.utils.ScreenUtils


class AuditAdUtilsNew {

    companion object {

        fun openCSJSplashAd(
            context: Context,
            appId: String,
            appName: String,
            splashId: String,
            mView: ViewGroup,
            splashAdListener: onSplashAdListener
        ) {
            if (TextUtils.isEmpty(appId) || TextUtils.isEmpty(splashId)) {
                return
            }
            TTSdkNew.init(
                context,
                appId,
                appName
            )
            //创建TTAdNative对象，createAdNative(Context context) context需要传入Activity对象
            val mTTAdNative = TTAdSdk.getAdManager().createAdNative(context)
            val adSlot = AdSlot.Builder()
                .setCodeId(splashId) //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告，请在穿山甲平台查看。且仅是模板渲染的代码位ID使用，非模板渲染代码位切勿使用
                .setExpressViewAcceptedSize(
                    ScreenUtils.getScreenWidth(context).toFloat(),
                    ScreenUtils.getScreenHeight(context).toFloat()
                )
                .build()
            mTTAdNative.loadSplashAd(adSlot, object : TTAdNative.SplashAdListener {
                override fun onError(i: Int, s: String) {
                    splashAdListener?.splashEnd()
                    mView.removeAllViews()
                }

                override fun onTimeout() {
                    mView.removeAllViews()
                    splashAdListener?.splashEnd()
                }

                override fun onSplashAdLoad(ttSplashAd: TTSplashAd) {
                    if (ttSplashAd != null) {
                        ttSplashAd.setSplashInteractionListener(object :
                            TTSplashAd.AdInteractionListener {
                            override fun onAdClicked(view: View, i: Int) {
                                mView.removeAllViews()
                                splashAdListener?.splashEnd()
                            }

                            override fun onAdShow(view: View, i: Int) {
                            }

                            override fun onAdSkip() {
                                mView.removeAllViews()
                                splashAdListener?.splashEnd()
                            }

                            override fun onAdTimeOver() {
                                mView.removeAllViews()
                                splashAdListener?.splashEnd()
                            }
                        })
                        val splashView = ttSplashAd.splashView
                        if (splashView != null) {
                            mView.visibility = View.VISIBLE
                            mView.removeView(splashView)
                            mView.addView(splashView)
                            return
                        }
                    }
                }
            }, 5000)

        }


    }

    interface onSplashAdListener {
        fun splashEnd()
    }
}